package com.sxj.springsourcecode.springframework.factory.support;

import com.sxj.springsourcecode.springframework.factory.config.BeanDefinition;
import com.sxj.springsourcecode.springframework.factory.config.BeanDefinitionRegistry;
import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: spring-source-code
 * @Package: com.sxj.springsourcecode.springframework.factory.support
 * @ClassName: DefaultListableBeanFactory
 * @Author: sxj
 * @Description:
 * @Date: 2024/2/22 23:25
 * @Version: 1.0.0
 */
public class DefaultListableBeanFactory extends ConfigurableListableBeanFactory implements BeanDefinitionRegistry {



    /**
     * 提前实例化单例bean
     *
     */
    @Override
    public void preInstantiateSingletons(){
        for (Map.Entry<String, BeanDefinition> entry : getBeanDefinitionMap().entrySet()) {
            String key = entry.getKey();
            getBean(key);
        }
    }


    @Override
    public <T> Map<String, T> getBeansOfType(Class<T> type) {
        Map<String, T> result = new HashMap<>();
        for (Map.Entry<String, BeanDefinition> entry : getBeanDefinitionMap().entrySet()) {
            String beanName = entry.getKey();
            BeanDefinition beanDefinition = entry.getValue();
            if (type.isAssignableFrom(beanDefinition.getClz())){
                T bean = (T) getBean(beanName);
                result.put(beanName,bean);
            }
        }
        return result;
    }

}
